package net.tngou.iask.action.my;


import java.util.List;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.At;
import net.tngou.iask.pojo.Msgs;
import net.tngou.iask.pojo.User;
import net.tngou.iask.service.MsgsService;
import net.tngou.iask.util.StringUtil;

/**
 * 消息
 * @author tngou@tngou.net
 *
 */
public class MsgsAction extends BaseAction {

	/**
	 * 显示聊天消息
	 */
	public void show() {
		
		if(ask.getId()==0)
		{
			run_404();return;
		}
		User u= new User().get(ask.getId());
		if(u==null)
		{
			run_404();return;
		}
		
		MsgsService service = new MsgsService();
		PageUtil page = service.getPage(user.getId(), u.getId(), ask.getPage(), ask.getSize());
		root.put("page", page);
		root.put("u", u);
		service.setRead(user.getId(), u.getId());
		printFreemarker("my/msgs_show.ftl", root);
	}
	
	/**
	 * 显示关系用户
	 */
	public void list() {
		
		List<?> list3 = service.getList("main="+user.getMain()+" AND role=3", User.class);
		List<?> list4 = service.getList("main="+user.getMain()+" AND role=4", User.class);
		List<?> list5 = service.getList("main="+user.getMain()+" AND role=5", User.class);
		List<?> list6 = service.getList("main="+user.getMain()+" AND role=6", User.class);
		root.put("list3", list3);
		root.put("list4", list4);
		root.put("list5", list5);
		root.put("list6", list6);
		printFreemarker("my/msgs_list.ftl", root);
	}
	
	/**
	 * 与我相关
	 */
	public void at() {
		
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), "user="+user.getId(), At.class);
		root.put("page", page);
		printFreemarker("my/msgs_at.ftl", root);
	}
	
	
	/**
	 * 显示AT消息
	 */
	public void atshow() {
		if(ask.getId()==0)
		{
			run_404();return;
		}
		At at = new At().get(ask.getId());
		if(at==null)
		{
			run_404();return;
		}
		String url=request.getParameter("url");
		if(StringUtil.isNotEmpty(url))
		{
			url=at.getUrl()+"?url="+url;
		}else
		{
			url=at.getUrl();
		}
		at.setIsread(1);//设置已读
		at.update();
		sendRedirect(url);
	}
	/**
	 * 系统提醒
	 */
	public void sys() {
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), "user="+user.getId()+" AND type=1", Msgs.class);
		root.put("page", page);
		printFreemarker("my/msgs_sys.ftl", root);
	}
	
	/**
	 * 系统消息
	 */
	public void sysshow() {
		if(ask.getId()==0)
		{
			run_404();return;
		}
		Msgs msgs = new Msgs().get(ask.getId());
		if(msgs==null)
		{
			run_404();return;
		}
		msgs.setIsread(1);//设置已读
		msgs.update();
		root.put("msgs", msgs);
		printFreemarker("my/msgs_sysshow.ftl", root);
	}
	
}
